package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.CategoryMapper;
import com.kgc.service.ICategoryService;
import entity.Category;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @description:   商品分类信息相关方法实现类
 *
 * @author: Cheng
 *
 * @create: 2022-04-13 10:57
 **/

@Service
@SuppressWarnings("all")
public class ICategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    private Logger logger = Logger.getLogger(this.getClass());


    /**
     * 分页查询商品分类信息
     * @return  ResponseMessage
     */
    @Override
    public ResponseMessage selectALL(int pageNo ,int pageSize,String cname ,int type ,int parentId) {
        logger.info("ICategoryServiceImpl selectALL is start ...params:pageNo-->"+pageNo+",pageSize-->"+pageSize+",canme-->"+cname+",type:-->"+type+",parentId-->"+parentId);
        ResponseMessage responseMessage = new ResponseMessage();
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("cname", cname);
            map.put("type", type);
            map.put("parentId", parentId);
            //通过pageHelp分页查询
            PageHelper pageHelper = new PageHelper();
            pageHelper.startPage(pageNo, pageSize);
            List<Category> categories = categoryMapper.selectAll(map);
//            List<Category> categories=categoryMapper.selectList(null);
            //将结果发给到pageInfo里面
            PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
            logger.debug("ICategoryServiceImpl selectALL result:" + categoryPageInfo);
            responseMessage.setCode(StatusCode.OK);
            responseMessage.setData(categoryPageInfo);
            responseMessage.setMessage("查询成功");
            responseMessage.setFlag(true);
            if (categories == null) {
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setData(null);
                responseMessage.setMessage("查询失败,值为空");
                responseMessage.setFlag(false);
                logger.error("ICategoryServiceImpl selectALL categories Value is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl selectALL is error",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("查询时出现错误");
            return responseMessage;
        }
        return responseMessage;
    }


    /**
     * 添加一条商品分类信息
     * @param category
     * @return  ResponseMessage
     */
    @Override
    public ResponseMessage insertCategoryInfo(Category category) {
        logger.info("ICategoryServiceImpl insertCategoryInfo is stat ....params:category-->"+category);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //添加数据
            Integer insertNum = categoryMapper.insert(category);
            logger.debug("ICategoryServiceImpl insertCategoryInfo result:"+insertNum);
            if (insertNum > 0) {       //添加成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("添加成功");
                responseMessage.setData(insertNum);
            } else {                //添加失败
                logger.error("ICategoryServiceImpl insertCategoryInfo is error ,The insertNum less than or equal to zero");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("添加失败");
                responseMessage.setData(insertNum);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl insertCategoryInfo is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("添加失败");
            responseMessage.setData(e);
            return responseMessage;

        }
        return responseMessage;
    }


    /**
     * 更新一条商品分类信息实现类
     * @param category
     * @return  ResponseMessage
     */
    @Override
    public ResponseMessage updateCategoryInfo(Category category) {
        logger.info("ICategoryServiceImpl updateCategoryInfo is stat ....params:category-->"+category);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //添加数据
            Integer insertNum = categoryMapper.updateById(category);
            logger.debug("ICategoryServiceImpl updateCategoryInfo result:"+insertNum);
            if (insertNum > 0) {       //修改成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("更新成功");
                responseMessage.setData(insertNum);
            } else {                //修改失败
                logger.error("ICategoryServiceImpl updateCategoryInfo is error ,The insertNum less than or equal to zero");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("更新失败");
                responseMessage.setData(insertNum);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl updateCategoryInfo is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("更新失败");
            responseMessage.setData(e);
            return responseMessage;
        }
        return responseMessage;
    }

    /**
     *删除单条商品分类
     * @param category
     * @return  ResponseMessage
     */
    @Override
    public ResponseMessage freezeCategoryInfo(String id) {
        logger.info("ICategoryServiceImpl freezeCategoryInfo is stat ....params:id-->"+id);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //这里先验证下数据,后台也查询
                //查询下面是否有商品
            int row = categoryMapper.selectProductByCategoryLevel1Id(id);
            logger.debug("ICategoryServiceImpl freezeCategoryInfo selectProductByCategoryLevel1Id result:row" + row);
            if (row>0) {        //大于0证明该分类下面还有商品
                logger.error("ICategoryServiceImpl freezeCategoryInfo selectProductByCategoryLevel1Id is error ,The row more than to zero,this category have product");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setFlag(false);
                responseMessage.setMessage("该分类下还有商品,无法删除!");
                return responseMessage;
            }
            //查询下面是否还有子分类
            int childrenCategoryCount = categoryMapper.selectChildrenCategory(id);
            logger.debug("ICategoryServiceImpl freezeCategoryInfo selectChildrenCategory result:row" + row);
            if (childrenCategoryCount>0) {        //大于0证明该分类下面还有分类
                logger.error("ICategoryServiceImpl freezeCategoryInfo selectChildrenCategory is error ,The row more than to zero,this category have ChildrenCategory");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setFlag(false);
                responseMessage.setMessage("该分类下还有子分类,无法删除!");
                return responseMessage;
            }

            //逻辑删除数据
            Integer insertNum = categoryMapper.freezeCategoryInfo(id);
            logger.debug("ICategoryServiceImpl freezeCategoryInfo result:"+insertNum);
            if (insertNum > 0) {       //删除成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("删除成功");
                responseMessage.setData(insertNum);
            } else {                //删除失败
                logger.error("ICategoryServiceImpl freezeCategoryInfo is error ,The insertNum less than or equal to zero");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("删除失败");
                responseMessage.setData(insertNum);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl freezeCategoryInfo is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("删除失败");
            responseMessage.setData(e);
            return responseMessage;
        }
        return responseMessage;
    }

    /**
     * 查询所有商品分类的type
     * @return List<Category>
     */
    @Override
    public List<Category> selectByCategoryType() {
        List<Category> typeList =null;
        try {
            logger.info("ICategoryServiceImpl selectByCategoryType is start...");
            typeList = categoryMapper.selectByCategoryType();
            logger.debug("ICategoryServiceImpl selectByCategoryType result:typeList:"+typeList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl selectByCategoryType is error",e);
        }
        return typeList;
    }


    /**
     * 通过type查找所有的商品分类信息
     * @param type
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectInsetPageByType(int type) {
        logger.info("ICategoryServiceImpl selectInsetPageByType is stat ....params:type-->"+type);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //查询数据
            List<Category> categoryList = categoryMapper.selectInsetPageByType(type);
            logger.debug("ICategoryServiceImpl selectInsetPageByType result:"+categoryList);
            if (categoryList != null ) {       //查询成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("查询成功");
                responseMessage.setData(categoryList);
            } else {                //查询失败
                logger.error("ICategoryServiceImpl selectInsetPageByType is error ,The categoryList is null");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl selectInsetPageByType is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("查询失败");
            responseMessage.setData(e);
            return responseMessage;
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage selectCategoryById(String id) {
        logger.info("ICategoryServiceImpl selectCategoryById is stat ....params:id-->"+id);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //查询数据
            Category category = categoryMapper.selectCategoryById(id);
            logger.debug("ICategoryServiceImpl selectCategoryById result:"+category);
            if (category != null ) {       //查询成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("查询成功");
                responseMessage.setData(category);
            } else {                //查询失败
                logger.error("ICategoryServiceImpl selectCategoryById is error ,The category is null");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("查询失败");
                responseMessage.setData(category);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl selectCategoryById is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("查询失败");
            responseMessage.setData(e);
            return responseMessage;
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage selectInsetPageByCname(String cname) {
        logger.info("ICategoryServiceImpl selectInsetPageByCname is stat ....params:cname-->"+cname);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //查询数据
            Category category = categoryMapper.selectInsetPageByCname(cname);
            logger.debug("ICategoryServiceImpl selectInsetPageByCname result:"+category);
            if (category != null ) {       //查询成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("查询成功");
                responseMessage.setData(category);
            } else {                //查询失败
                logger.error("ICategoryServiceImpl selectInsetPageByCname is error ,The category is null");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("查询失败");
                responseMessage.setData(category);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl selectInsetPageByCname is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("查询失败");
            responseMessage.setData(e);
            return responseMessage;
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage selectUpdatePageByType(int type, String id) {
        logger.info("ICategoryServiceImpl selectUpdatePageByType is stat ....params:type-->"+type+",id:->"+id);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("type", type);
            //查询数据
            List<Category> categoryList = categoryMapper.selectUpdatePageByType(map);
            logger.debug("ICategoryServiceImpl selectUpdatePageByType result:"+categoryList);
            if (categoryList != null ) {       //查询成功
                responseMessage.setFlag(true);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("查询成功");
                responseMessage.setData(categoryList);
            } else {                //查询失败
                logger.error("ICategoryServiceImpl selectUpdatePageByType is error ,The categoryList is null");
                responseMessage.setFlag(false);
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("ICategoryServiceImpl selectUpdatePageByType is error",e);
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("查询失败");
            responseMessage.setData(e);
            return responseMessage;
        }
        return responseMessage;
    }

}
